/* 
 * 定义itfreer.arcgis.locator定位器模块
 */
(function(window, angular, undefined){
	/* 
	 * 定义ArcGIS定位器模块
	 */
	var itfreerarcgislocatorModule = angular.module('itfreer.arcgis.locator', ['itfreer.arcgis.core','itfreer.arcgis.geometryservice']);

	/* 
	 * 定义ArcGIS定位器模块
	 */
	itfreerarcgislocatorModule.factory('$itfreer_arcgis_locator', 
		function($rootScope, $itfreer_core, $itfreer_arcgis_core, 
			$itfreer_arcgis_geometryservice, $gisObject) {
			$itfreer_arcgis_locator = {
				/* 
				 * 当定位完成事件
				 */
				onAddressToLocationsCompleteEventFunction : null,

				/* 
				 * 初始化定位器
				 */
				init : function(){
					dojo.connect($gisObject.locator, "onAddressToLocationsComplete", this.onAddressToLocationsComplete);
				},

				/* 
				 * 当定位完成事件
				 */
				onAddressToLocationsComplete : function(candidate){
					var onFunction = $itfreer_arcgis_locator.onAddressToLocationsCompleteEventFunction 
						? $itfreer_arcgis_locator.onAddressToLocationsCompleteEventFunction 
						: $itfreer_arcgis_locator.defulatOnAddressToLocationsComplete;

					onFunction(candidate);
				},

				/* 
				 * 默认事件
				 */
				defulatOnAddressToLocationsComplete : function(candidate) {
		            if (candidate && candidate.length>0) {
		            	var adr = candidate[0];
		                var location = adr.location;
		                $rootScope.$apply(function() {
		            		$gisObject.locatorAddress = adr.address;
		            	});
		                var level = $gisObject.locatorLevel ? $gisObject.locatorLevel : 8;
		                $gisObject.map.centerAndZoom(location, level);
		            }
		        },

		        /* 
				 * 定位到指定名称的地址
				 */
				addressToLocations : function(name, level){
					if(!$itfreer_core.isNullOrUndefined($gisObject.locator)){
						$gisObject.locatorLevel = level;
						$gisObject.locator.addressToLocations({SingleKey:name});
					}
				},

				/* 
				 * 经纬度定位
				 */
				jwdLocator : function(){
					$itfreer_arcgis_draw.deDrawAndClearGraphics();

					var lonD = parseInt($gisObject.locatorParameter.jdd);
					var lonM = parseInt($gisObject.locatorParameter.jdf);
					var lonS = parseInt($gisObject.locatorParameter.jdm);
					var latD = parseInt($gisObject.locatorParameter.wdd);
					var latM = parseInt($gisObject.locatorParameter.wdf);
					var latS = parseInt($gisObject.locatorParameter.wdm);

					if (lonD > 180 || lonD < 0) {
						alert("【经度】:'度'范围 必须介于0~180之间,请重新输入");
						return;
					}
					if (latD > 90 || latD < 0) {
						alert("【纬度】:'度'范围必须介于0~90之间,请重新输入");
						return;
					}
					if (lonM > 60 || latM > 60 || lonM < 0 || latM < 0) {
						alert("'分' 范围必须介于0~60之间,请重新输入");
						return;
					}
					if (lonS > 60 || latS > 60 || lonS < 0 || latS < 0) {
						alert("'秒' 范围必须介于0~60之间,请重新输入");
						return;
					}

					var lon = $itfreer_arcgis_core.DMSConvertToDegree(lonD, lonM, lonS);
					var lat = $itfreer_arcgis_core.DMSConvertToDegree(latD, latM, latS);

					if (lat == "" || lon == "") {
						alert("请输入正确的经纬度");
						return;
					}

					var rPoint = new esri.geometry.Point();
					rPoint.x = lon;
					rPoint.y = lat;
					rPoint.spatialReference = $gisObject.dlwkid;

					// 如果是地理坐标，就直接定位
					if($gisObject.mapisdlwkid){
						$itfreer_arcgis_locator.coorLocation(rPoint);
					}else{
						// 先转换，再定位
						$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction = function(graphics){
							$itfreer_arcgis_locator.coorLocation(graphics[0]);
						};
						$itfreer_arcgis_geometryservice.project([rPoint], $gisObject.tywkid);
					}
				},

				/* 
				 * 地理坐标定位
				 */
				dlLocator : function(){
					$itfreer_arcgis_draw.deDrawAndClearGraphics();
					
					var x = parseFloat($gisObject.locatorParameter.dlx);
					var y = parseFloat($gisObject.locatorParameter.dly);

					var rPoint = new esri.geometry.Point();
					rPoint.x = x;
					rPoint.y = y;
					rPoint.spatialReference = $gisObject.dlwkid;

					// 如果是地理坐标，就直接定位
					if($gisObject.mapisdlwkid){
						$itfreer_arcgis_locator.coorLocation(rPoint);
					}else{
						// 先转换，再定位
						$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction = function(graphics){
							$itfreer_arcgis_locator.coorLocation(graphics[0]);
						};
						$itfreer_arcgis_geometryservice.project([rPoint], $gisObject.tywkid);
					}
				},

				/* 
				 * 平面坐标定位
				 */
				pmLocator : function(){
					$itfreer_arcgis_draw.deDrawAndClearGraphics();

					var x = parseFloat($gisObject.locatorParameter.pmx);
					var y = parseFloat($gisObject.locatorParameter.pmy);

					var rPoint = new esri.geometry.Point();
					rPoint.x = x;
					rPoint.y = y;
					rPoint.spatialReference = $gisObject.dlwkid;

					// 如果是地理坐标，先转换，再定位
					if($gisObject.mapisdlwkid){
						$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction = function(graphics){
							$itfreer_arcgis_locator.coorLocation(graphics[0]);
						};
						$itfreer_arcgis_geometryservice.project([rPoint], $gisObject.dlwkid);
					}else{
						// 就直接定位
						$itfreer_arcgis_locator.coorLocation(rPoint);
					}
				},

				/* 
				 * 坐标定位
				 */
				coorLocation : function(rPoint){
					var _extent = $gisObject.fullExtent;
					if (!_extent.contains(rPoint)) {
						alert("坐标信息不在范围之内");
						return;
					}

					$gisObject.map.centerAndZoom(rPoint);

					// 设置小气泡
					var iconURL = getJsBasePath()+"/arcgis/images/Fore_3D_Menu_ZBDW.png";
					var pMark = new esri.symbol.PictureMarkerSymbol(iconURL, 25, 25);
					var _center = $itfreer_arcgis_core.getCenter(rPoint);
					var graphic = new esri.Graphic(_center, pMark);
					$gisObject.map.graphics.add(graphic);
				}
			};
			for(f in $itfreer_arcgis_locator){
				$rootScope[f] = $itfreer_arcgis_locator[f];
			}
			return $itfreer_arcgis_locator;
		}
	);

	/* 
	 * 加载服务器
	 */
	itfreerarcgislocatorModule.run(function($rootScope, $itfreer_arcgis_locator) {
	  	
	});
}(window, angular));